package com.morkalork.swing;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.morkalork.entities.Brick;
import com.morkalork.entities.Color;

public class BrickManager
{	
	private final int BRICK_OFFSET = 70;
	private List<Brick> m_Bricks;
	
	public BrickManager()
	{
		m_Bricks = new ArrayList<Brick>();
		generateBrickList();
	}
	
	private void generateBrickList()
	{
		Random random = new Random();
		
		for(int row = 0; row < 10; row++)
		{
			for(int col = 0; col < 10; col++)
			{
				Color color = Color.values()[random.nextInt(3)];
				int x = (col * 20) + BRICK_OFFSET;
				int y = (row * 20) + BRICK_OFFSET;
				Brick brick = new Brick(new Point(x,  y), color);
				brick.setOriginalPoint(new Point(x, y));
				brick.setOriginalPoint(new Point(x, y));
				m_Bricks.add(brick);
			}
		}
	}

	public List<Brick> getBricks()
	{
		return m_Bricks;
	}
	
	public void flood(Brick clicked)
	{
		int index = m_Bricks.indexOf(clicked);
		if(index > 0)
		{
			//Check top
			for(Brick brick : m_Bricks)
			{
				if(brick != clicked)
				{
					if(brick.getCurrentPoint().getX() == clicked.getCurrentPoint().getX() && 
							(int)(brick.getCurrentPoint().getY() + 1) == (int)clicked.getCurrentPoint().getY())
					{
						if(brick.getColor() == clicked.getColor())
						{
							System.out.format("%s == %s", brick.getColor(), clicked.getColor()); 	
						}
					}
				}
			}
		}
	}
}
